<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class PlateController extends BaseController {

	public function cadd(){

		$this->typelistinfo();
		$this->display();
	}

	/*
	 * 分类列表
	 */
	public function clist(){

		$ys = D(C('TABLE_PLATERESOURCE'));
		$count = $ys->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $ys->order(array('y_datetime'=>'DESC'))->limit(limitbakstr($obj))->select();
		$this->assign('page',$show);
		$this->assign('datalist',$result);
		$this->display();
	}

	/*
	 * 添加
	 */
	public function resadd(){
		$ys = D(C('TABLE_PLATERESOURCE'));
		$rules = array(
			array('y_title','require','分类名称不能为空！'),
		);
		if(!$ys->validate($rules)->create()){
			$this->error($ys->getError(),'',1);
		}else{
			$ys->y_img = html_entity_decode(I("post.y_img"));
			$ys->y_vurl = html_entity_decode(I("post.y_vurl"));
			$ys->content = html_entity_decode(I('post.content'));
			$result = $ys->add();
			if($result){
				$this->success('添加成功','/Admin/Plate/clist');
			}else{
				$this->error('添加失败','',1);
			}
		}
	}

    public function type(){

		$this->typelistinfo();
    	$this->display();
    }

	protected function typelistinfo(){
		$resource = D(C('TABLE_PLATE'));
		$res = $resource->select();
		$this->assign('data',$res);
	}
    
    public function resourceadd(){
    	$this->assign('resourcelist',$this->getresourtype());
    	$this->display();
    }
    
    public function plateedit(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$resource = D(C('TABLE_PLATE'));
    		$res = $resource->find($id);
    		if($res){
    			$this->assign('resourcearticle',$res);
    			$this->display();
    		}else{
    			$this->error('无数据可用',1);
    		}
    		
    	}else{
    		$this->error('参数错误',1);
    	}
    }
    
    /*
     * 删除
     */
    public function platedelete(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$resource = D(C('TABLE_PLATE'));
    		$res = $resource->delete($id);
    		if($res){
    			$this->success('删除成功！');
    		}
    	}else{
    		$this->error('参数错误',1);
    	}
    }


	/*
	 * 编辑页
	 */
	public function cedit(){
		$id = I('get.id');
		if(!empty($id)){
			$ys = D(C('TABLE_PLATERESOURCE'));
			$resutl = $ys->find($id);
			$this->assign('datainfo',$resutl);
			$this->typelistinfo();
			$this->display();
		}else{
			$this->error('非法参数','',1);
		}
	}


	public function pdelete(){
		$id = I('get.id');
		if(!empty($id)){
			$resource = D(C('TABLE_PLATERESOURCE'));
			$res = $resource->delete($id);
			if($res){
				$this->success('删除成功！');
			}
		}else{
			$this->error('参数错误',1);
		}
	}
    
    /*
     * 列出资源分类
     */
    public function getresourtype(){
    	$resource = D(C('TABLE_PLATE'));
		$res = $resource->select();
    	return $res;
    }
    
    /*
     * 添加资源栏目
     */
    public function plateaddinfo(){
    	$rules = array(
    			array('typename','require','板块名称不能为空！'),
    	);
    	$resource = D(C('TABLE_PLATE'));
    	if(!$resource->validate($rules)->create()){
    		$this->error($resource->getError(),'',1);
    	}else{
			$resource->typedate = date("Y-m-d H:i:m");
			$result = $resource->add();
			if($result){
				$this->success('添加成功','/Admin/Plate/type');
			}else{
				$this->error('添加失败','',1);
			}
    	}
    }
    
    /*
     * 修改分类资源
     */
    public function pedit(){
    	$id = I('post.editid');
    	$rules = array(
    		array('y_title','require','分类名称不能为空！'),
    	);
    	$resource = D(C('TABLE_PLATERESOURCE'));
    	if(!$resource->validate($rules)->create()){
    		$this->error($resource->getError(),'',1);
    	}else{
    		$result = $resource->where(array('id'=>$id))->save();
    		if($result){
    			$this->success('修改成功','/Admin/Plate/clist');
    		}else if($result==0){
				header('Location:/Admin/Plate/clist');
			}else{
    			$this->error('修改失败','',1);
    		}
    	}
    }
  
    
}